package ejb.cbm.admin.session;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ejb.cbm.admin.entity.CentroCostoProtegidoEntity;
import ejb.cbm.admin.local.CentroCostoProtegidoSessionLocal;

////@Stateless

public class CentroCostoProtegidoSession extends HibernateDaoSupport implements CentroCostoProtegidoSessionLocal {
	
	static Logger logger = Logger.getLogger(CentroCostoProtegidoSession.class.getName());
	//property constants
	public static final String FLAGACTIVO = "flagactivo";
	public static final String USUARIOREGISTRO = "usuarioregistro";
	public static final String USUARIOMODIFICACION = "usuariomodificacion";


    //@PersistenceContext 
    private EntityManager entityManager;
    public void save(CentroCostoProtegidoEntity transientInstance) {
    	logger.log("saving CentroCostoProtegidoEntity instance", Level.INFO, null,null);
        try {
            entityManager.persist(transientInstance);
            logger.log("save successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("save failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public void delete(CentroCostoProtegidoEntity persistentInstance) {
    	logger.log("deleting CentroCostoProtegidoEntity instance", Level.INFO, null,null);
        try {
            entityManager.remove(persistentInstance);
            logger.log("delete successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("delete failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public CentroCostoProtegidoEntity update(CentroCostoProtegidoEntity detachedInstance) {
    	logger.log("updating CentroCostoProtegidoEntity instance", Level.INFO, null,null);
        try {
            CentroCostoProtegidoEntity result = entityManager.merge(detachedInstance);
            logger.log("update successful", Level.INFO, null,null);
            return result;
        } catch (RuntimeException re) {
        	logger.log("update failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public CentroCostoProtegidoEntity findById( Long id) {
    	logger.log("finding CentroCostoProtegidoEntity instance with id: " + id, Level.INFO, null,null);
        try {
            CentroCostoProtegidoEntity instance = entityManager.find(CentroCostoProtegidoEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
        	logger.log("find failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    //@SuppressWarnings("unchecked")
    public List<CentroCostoProtegidoEntity> findByProperty(String propertyName, Object value) {
    	logger.log("finding CentroCostoProtegidoEntity instance with property: " + propertyName
    						+ ", value: " + value, Level.INFO, null,null);
		try {
			String queryString = "select model from CentroCostoProtegidoEntity model where model." 
			 						+ propertyName + "= :propertyValue";
			return entityManager.createQuery(queryString)
 				.setParameter("propertyValue", value).getResultList();
		} catch (RuntimeException re) {
			logger.log("find by property name failed", Level.FATAL,null, re);
			throw re;
		}
	}
	
	public List<CentroCostoProtegidoEntity> findByFlagactivo(Object flagactivo) {
		return findByProperty(FLAGACTIVO, flagactivo);
	}
	
	public List<CentroCostoProtegidoEntity> findByUsuarioregistro(Object usuarioregistro) {
		return findByProperty(USUARIOREGISTRO, usuarioregistro);
	}
	
	public List<CentroCostoProtegidoEntity> findByUsuariomodificacion(Object usuariomodificacion) {
		return findByProperty(USUARIOMODIFICACION, usuariomodificacion);
	}
	

	//@SuppressWarnings("unchecked")
	public List<CentroCostoProtegidoEntity> findAll() {
		logger.log("finding all CentroCostoProtegidoEntity instances", Level.INFO, null,null);
		try {
			String queryString = "select model from CentroCostoProtegidoEntity model";
			return entityManager.createQuery(queryString).getResultList();
		} catch (RuntimeException re) {
			logger.log("find all failed", Level.FATAL,null, re);
			throw re;
		}
	}
}